//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Text;
using WCSFContrib.PageFlow;
using WCSFContrib.PageFlow.Utils;

namespace WCSFContrib.PageFlow.Xml
{
    public class XmlPageFlowRuntime
    {
        private IXmlPageFlowInstanceStore _xmlPageFlowInstanceStore;

        public XmlPageFlowRuntime(IXmlPageFlowInstanceStore xmlPageFlowInstanceStore)
        {
            _xmlPageFlowInstanceStore = xmlPageFlowInstanceStore;
        }

        public virtual XmlPageFlowInstance CreatePageFlow(Type pageFlowType)
        {
            XmlPageFlowInstance instance = new XmlPageFlowInstance(Guid.NewGuid(), pageFlowType, this);
            _xmlPageFlowInstanceStore.Add(instance);
            return instance;
        }

        public virtual XmlPageFlowInstance GetPageFlow(Guid instanceId)
        {
            XmlPageFlowInstance instance = _xmlPageFlowInstanceStore.Get(instanceId);

            if (instance != null)
            {
                instance.Runtime = this;
            }

            return instance;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
        public virtual void Terminate(XmlPageFlowInstance instance)
        {
            Guard.ArgumentNotNull(instance, "instance");
            _xmlPageFlowInstanceStore.Remove(instance.InstanceId);
        }

        public virtual void Persist(XmlPageFlowInstance instance)
        {
            Guard.ArgumentNotNull(instance, "instance");
            _xmlPageFlowInstanceStore.Update(instance);
        }
    }
}
